//
//  YTAssociateDriverViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/8/10.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAssociateDriverViewController.h"
#import "YTSelecteDriverViewCell.h"
#import "YTDriverTeamServices.h"
#import "YTTeamDriverModel.h"

@interface YTAssociateDriverViewController ()
@property (assign, nonatomic) NSInteger page;
@property (strong, nonatomic) NSMutableArray<YTTeamDriverModel *> *dataArray;
@end

@implementation YTAssociateDriverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _dataArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"YTSelecteDriverViewCell" bundle:nil] forCellReuseIdentifier:@"YTSelecteDriverViewCell"];
    self.tableView.rowHeight = 110;
    [self requestData];
    @weakify(self);
    [self.tableView addLoadDataControl:^{
        @strongify(self);
        self.page++;
    }];
}

/** 网络请求*/
- (void)requestData {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"userId"] = [YTUtilityManager manager].userModel.userId;
    dict[@"page"] = [NSString stringWithFormat:@"%zd", self.page];
    [[YTDriverTeamServices loadDriverList:dict] subscribeNext:^(NSDictionary *response) {
        NSDictionary *data = response[@"responseData"];
        NSArray<YTTeamDriverModel *> *array = [NSArray yy_modelArrayWithClass:[YTTeamDriverModel class] json:data[@"dataList"]];
        [self.dataArray addObjectsFromArray:array];
        [self.tableView reloadData];
        if (array.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - Table view data source 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTSelecteDriverViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTSelecteDriverViewCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_callBackAction) {
        _callBackAction(_dataArray[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
